import Vuex from 'vuex'
import Vue from 'vue'
import { USER_SPECICAL_INFO} from '@/utils/constant'
import user from './modules/user'
import myPlugin from './plugins/myPlugin'

Vue.use(Vuex);

export const store = new Vuex.Store({
  modules: {
    user,
  },
  plugins: [myPlugin],
  state: {
    userInfo: {
      userStatus: 'loading'
    },
    systemInfo: {},
  },
  mutations: {
    setUserInfo(state, payload) {
      console.log('Vuex setUserInfo: %o', payload);
      state.userInfo = payload;
    },
  },
  actions: {
    readLocalUserInfo({ commit }) {
      let localUserInfo = uni.getStorageSync(USER_SPECICAL_INFO) || {};
      // 本地信息有效才往往全局变量存
      if (localUserInfo.minimember_id) {
        commit('setUserInfo', localUserInfo);
      }
    },
  }
});